#include "CUserTask.h"
#include "../mainctrl.h"
#include "commmsg.h"

void CUserTask::Clean()
{
    CT_TRACE(("清理 UserTask"));
    
    _stTaskStatus.Clear();
    _astOpenTaskIDs.Clear();
    _astTaskNeedAccept.Clear();
    _astValidTasks.Clear();
    _astWaitForOpenTasks.Clear();

    _astLevelUpTriggers.Clear();
    _astPetLevelUpTriggers.Clear();
    _astBossConquerTriggers.Clear();
    _astFinishTaskTriggers.Clear();
    _astKillEnemyTriggers.Clear();
    _astDoSomeOptTriggers.Clear();
    _astUseItemTriggers.Clear();
    _astGetItemTriggers.Clear();
    _astFinishInstanceTriggers.Clear();
    m_bTaskTargetStatus = false;
}

int CUserTask::AddOpenTaskData(CTaskID& stTaskID)
{
    if (_astOpenTaskIDs.Size() >= MAX_OPEN_TASK)
    {
        return ERROR_TASK_OPEN_TASK_FULL;
    }

    _astOpenTaskIDs.AddOneItem(stTaskID);
    return 0;
}

int CUserTask::AddValidTaskData(CValidTask& stValidTask)
{
    if (_astValidTasks.Size() >= MAX_VALID_TASK)
    {
        return ERROR_TASK_FULL;
    }

    int iIndex = _astValidTasks.AddOneItem(stValidTask);
    CT_TRACE(("任务索引 %d", iIndex));
    return AddValidTaskTrigger((unsigned char)(iIndex & 0x7F), stValidTask);
}

int CUserTask::DelValidTaskData(CTaskID& stTaskID)
{
    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        if (stTaskID == _astValidTasks[i]._stTaskID)
        {
            CValidTask& stValidTask = _astValidTasks[i];
            
            for (int j = 0; j < stValidTask._astTargetStats.Size(); ++j)
            {
                CleanTrigger(stValidTask._astTargetStats[j]._stCond._ucCond, stValidTask._stTaskID);
            }

            _astValidTasks.DelOneItem(i);
        }
    }

    return 0;
}

int CUserTask::AddWaitForOpenData(CTaskWaitForOpen& stWaitForOpen)
{
    if (_astWaitForOpenTasks.Size() >= MAX_WAIT_FOR_OPEN_TASK)
    {
        return ERROR_TASK_WAIT_FOR_OPEN_TASK_FULL;
    }

    int iIndex = _astWaitForOpenTasks.AddOneItem(stWaitForOpen);
    return AddWaitForOpenTrigger((unsigned char) iIndex, stWaitForOpen);
}

int CUserTask::AddValidTaskTrigger(unsigned char ucTaskIndex, CValidTask& stValidTask)
{
    int iRet = 0;
    CTaskTargetTrigger stTrigger;
    stTrigger._ucTargetType = CTaskTargetTrigger::TASK_TRIGGER_TARGET_FINISH;
    stTrigger._stTaskID = stValidTask._stTaskID;
    stTrigger._ucTaskIndex = ucTaskIndex;

    for (unsigned char i = 0 ; i < stValidTask._astTargetStats.Size(); ++i)
    {
        stTrigger._ucTargetIndex = i;
        iRet = AddTrigger(stValidTask._astTargetStats[i]._stCond._ucCond, stTrigger);
        CHECK_RETURN(iRet);
    }

    return 0;
}

int CUserTask::AddWaitForOpenTrigger(unsigned char ucTaskIndex, CTaskWaitForOpen& stWaitForOpen)
{
    int iRet = 0;
    CTaskTargetTrigger stTrigger;
    stTrigger._ucTargetType = CTaskTargetTrigger::TASK_TRIGGER_TARGET_OPEN;
    stTrigger._stTaskID = stWaitForOpen._stTaskID;
    stTrigger._ucTaskIndex = ucTaskIndex;

    for (unsigned char i = 0 ; i < stWaitForOpen._astTargetStats.Size(); ++i)
    {
        stTrigger._ucTargetIndex = i;
        iRet = AddTrigger(stWaitForOpen._astTargetStats[i]._stCond._ucCond, stTrigger);
        CHECK_RETURN(iRet);
    }

    return 0;
}

int CUserTask::AddTrigger(unsigned char ucCondType, CTaskTargetTrigger& stTrigger)
{
    int iRet = -1;

    switch(ucCondType)
    {
    case CTaskCond::TASK_LEVEL_UP:
        iRet = AddLevelUpTrigger(stTrigger);
        break;
    case CTaskCond::TASK_BOSS_CONQUER:
        iRet = AddBossConquerTrigger(stTrigger);
        break;
    case CTaskCond::TASK_TASK_FINISH:
        iRet = AddTaskFinishTrigger(stTrigger);
        break;
    case CTaskCond::TASK_PET_LEVEL_UP:
        iRet = AddPetLevelUpTrigger(stTrigger);
        break;
    case CTaskCond::TASK_ENEMY_KILL:
        iRet = AddKillEnemyTrigger(stTrigger);
        break;
    case CTaskCond::TASK_DO_SOME_OPT:
        iRet = AddDoSomeOptTrigger(stTrigger);
        break;
    case CTaskCond::TASK_USE_ITEM:
        iRet = AddUseItemTrigger(stTrigger);
        break;
    case CTaskCond::TASK_GET_ITEM:
    case CTaskCond::TASK_GET_AND_COMMIT_ITEM:
        iRet = AddGetItemTrigger(stTrigger);
        break;
    case CTaskCond::TASK_FINISH_STAGE:
        iRet = AddFinishInstanceTrigger(stTrigger);
        break;
    default:
        iRet = ERROR_TASK_TRIGGER_COND_TYPE;
    }

    CT_TRACE(("添加触发器: 条件类型 %d 任务索引 %d 条件索引 %d 触发器类型 %d",
        ucCondType, stTrigger._ucTaskIndex, stTrigger._ucTargetIndex, stTrigger._ucTargetType));

    return iRet;
}

int CUserTask::AddLevelUpTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astLevelUpTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astLevelUpTriggers.AddOneItem(stTrigger);
    return 0;
}

int CUserTask::AddBossConquerTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astBossConquerTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astBossConquerTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddTaskFinishTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astFinishTaskTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astFinishTaskTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddPetLevelUpTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astPetLevelUpTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astPetLevelUpTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddKillEnemyTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astKillEnemyTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astKillEnemyTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddDoSomeOptTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astDoSomeOptTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astDoSomeOptTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddUseItemTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astUseItemTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astUseItemTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddGetItemTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astGetItemTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astGetItemTriggers.AddOneItem(stTrigger);

    return 0;
}

int CUserTask::AddFinishInstanceTrigger(CTaskTargetTrigger& stTrigger)
{
    if (_astFinishInstanceTriggers.Size() >= MAX_TRIGGER_PER_TYPE)
    {
        return ERROR_TASK_TRIGGER_FULL;
    }

    _astFinishInstanceTriggers.AddOneItem(stTrigger);

    return 0;
}

//升级触发
int CUserTask::TriggerLevelUp(int iNowLevel)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_LEVEL_UP;
    stCond._iCondData1 = iNowLevel;

    for (int i = 0; i < _astLevelUpTriggers.Size(); ++i)
    {
        iRet = _astLevelUpTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::TriggerTaskFinish(CTaskID& stTaskID)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_TASK_FINISH;
    stCond._iCondData1 = stTaskID._uiBranchID * TASK_CONFIG_ID_MOD + stTaskID._uiTaskID;

    for (int i = 0; i < _astFinishTaskTriggers.Size(); ++i)
    {
        iRet = _astFinishTaskTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::TriggerConquerBoss(int iBossID, int iCount)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_BOSS_CONQUER;
    stCond._iCondData1 = iBossID;
    stCond._iCondData2 = iCount;

    for (int i = 0; i < _astBossConquerTriggers.Size(); ++i)
    {
        iRet = _astBossConquerTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::TriggerPetLevelUp(int iNowPetLevel)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_PET_LEVEL_UP;
    stCond._iCondData1 = iNowPetLevel;

    for (int i = 0; i < _astPetLevelUpTriggers.Size(); ++i)
    {
        iRet = _astPetLevelUpTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::TriggerKillEnemy(int iEnemyID, int iCount)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_ENEMY_KILL;
    stCond._iCondData1 = iEnemyID;
    stCond._iCondData2 = iCount;

    for (int i = 0; i < _astKillEnemyTriggers.Size(); ++i)
    {
        iRet = _astKillEnemyTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::TriggerDoSomeOpt(int iOptID, int iCount)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_DO_SOME_OPT;
    stCond._iCondData1 = iOptID;
    stCond._iCondData2 = iCount;

    for (int i = 0; i < _astDoSomeOptTriggers.Size(); ++i)
    {
        iRet = _astDoSomeOptTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::TriggerUseItem(unsigned char ucItemType, unsigned short usItemID, int iCount)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_USE_ITEM;
    stCond._iCondData1 = ucItemType * ITEM_CONFIG_ID_MOD + usItemID;
    stCond._iCondData2 = iCount;

    for (int i = 0; i < _astUseItemTriggers.Size(); ++i)
    {
        iRet = _astUseItemTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }
    this->UpdateTaskStatus();

    return 0;
}

int CUserTask::TriggerGetItem(unsigned char ucItemType, unsigned short usItemID, int iCount)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_USE_ITEM;
    stCond._iCondData1 = ucItemType * ITEM_CONFIG_ID_MOD + usItemID;
    stCond._iCondData2 = iCount;

    for (int i = 0; i < _astGetItemTriggers.Size(); ++i)
    {
        iRet = _astGetItemTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    stCond._ucCond = CTaskCond::TASK_GET_ITEM;
    for (int i = 0; i < _astGetItemTriggers.Size(); ++i)
    {
        iRet = _astGetItemTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    stCond._ucCond = CTaskCond::TASK_GET_AND_COMMIT_ITEM;
    for (int i = 0; i < _astGetItemTriggers.Size(); ++i)
    {
        iRet = _astGetItemTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}


int CUserTask::TriggerFinishInstance(int iInstanceID, int iCount)
{
    int iRet = 0;

    CTaskCond stCond;
    stCond._ucCond = CTaskCond::TASK_FINISH_STAGE;
    stCond._iCondData1 = iInstanceID;
    stCond._iCondData2 = iCount;

    for (int i = 0; i < _astFinishInstanceTriggers.Size(); ++i)
    {
        iRet = _astFinishInstanceTriggers[i].Trigger(*this, stCond);
        CHECK_RETURN(iRet);
    }

    this->UpdateTaskStatus();
    return 0;
}

int CUserTask::UpdateTaskStatus()
{
    //检查待激活任务是否可以转变为激活任务，如果可以，将任务激活
    int iRet = CheckWaitForOpenTask();
    CHECK_RETURN(iRet);

    iRet = CheckValidTask();
    CHECK_RETURN(iRet);

    if( this->m_bTaskTargetStatus )
    {
        int iIndex = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash.HashFind(this->GetUserGid());
        if (iIndex < 0)
        {
            CT_ERROR(("user %u not in hash", this->GetUserGid()));
            return -1;
        }
        CPlayerBaseInfo& stPlayerBaseInfo = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash[iIndex];

        iRet = this->GetData( stPlayerBaseInfo._stUserTaskData);
        CHECK_RETURN(iRet);

        //保存数据
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = stPlayerBaseInfo._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);

        //下发任务数据
        iRet = UpdateTaskList2Front();
        CHECK_RETURN(iRet);
    }

    this->m_bTaskTargetStatus = false;

    return 0;
}

//
int CUserTask::UpdateValidTask(CTaskTargetTrigger& stTaskTargetTrigger, CTaskCond& stCond)
{        
    int iRet = 0;   

    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        if (stTaskTargetTrigger._stTaskID == _astValidTasks[i]._stTaskID)
        {
            CTaskTargetStat& stValidTaskStat = _astValidTasks[i]._astTargetStats[stTaskTargetTrigger._ucTargetIndex];
            iRet = stValidTaskStat.Trigger(stCond, this->m_bTaskTargetStatus);
            CHECK_RETURN(iRet);
        }
    }

    return 0;
}

int CUserTask::UpdateTaskList2Front()
{
    CMainCtrl::m_pstMain->m_stNotifyMgr.Notify(GetUserGid(), NOTIFY_TASK_LIST);

    return RET_OK;
}

int CUserTask::UpdateWaitForOpenTask(CTaskTargetTrigger& stTaskTargetTrigger, CTaskCond& stCond)
{
    int iRet = 0;       
    
    for (int i = 0; i < _astWaitForOpenTasks.Size(); ++i)
    {
        if (stTaskTargetTrigger._stTaskID == _astWaitForOpenTasks[i]._stTaskID)
        {
            CTaskTargetStat& stWaitForOpenTaskStat = 
                _astWaitForOpenTasks[i]._astTargetStats[stTaskTargetTrigger._ucTargetIndex];

            iRet = stWaitForOpenTaskStat.Trigger(stCond, this->m_bTaskTargetStatus);
            CHECK_RETURN(iRet);
        }
    }

    return 0;
}

//检查等待激活的任务，如果满足激活条件则激活
int CUserTask::CheckWaitForOpenTask()
{
    int iRet = 0;

    for (unsigned char i = 0; i < _astWaitForOpenTasks.Size(); ++i)
    {
        for (unsigned char j = 0; j < _astWaitForOpenTasks[i]._astTargetStats.Size(); ++j)
        {
            if (_astWaitForOpenTasks[i]._astTargetStats[j].IsReachCond())
            {
                continue;
            }

            iRet = _astWaitForOpenTasks[i]._astTargetStats[j].CheckReachCond();
            CHECK_RETURN(iRet);

            
            if (_astWaitForOpenTasks[i]._astTargetStats[j].IsReachCond())
            {
                m_bTaskTargetStatus = true;
            }

            /*
            if (_astWaitForOpenTasks[i]._astTargetStats[j].IsReachCond())
            {
                iRet = CleanTrigger(_astWaitForOpenTasks[i]._astTargetStats[j]._stCond._ucCond, _astWaitForOpenTasks[i]._stTaskID);
                CHECK_RETURN(iRet);
            }
            */
        }   

        if (_astWaitForOpenTasks[i].IsCanOpen())
        {
            //清理触发器
            for (unsigned char j = 0; j < _astWaitForOpenTasks[i]._astTargetStats.Size(); ++j)
            {
                iRet = CleanTrigger(_astWaitForOpenTasks[i]._astTargetStats[j]._stCond._ucCond, _astWaitForOpenTasks[i]._stTaskID);
                CHECK_RETURN(iRet);
            }

            if (_astWaitForOpenTasks[i].IsDirectAccept())
            {
                //等待接受
                _astTaskNeedAccept.AddOneItem(_astWaitForOpenTasks[i]._stTaskID);
            }
            else
            {
                //激活任务
                _astOpenTaskIDs.AddOneItem(_astWaitForOpenTasks[i]._stTaskID);
            }

            //清理待激活任务
            _astWaitForOpenTasks.ShiftDelOneItem(i);
            --i;
        }
    }

    return 0;
}

//检查进行中的任务
int CUserTask::CheckValidTask()
{
    int iRet = 0;

    for (unsigned char i = 0; i < _astValidTasks.Size(); ++i)
    {
        for (unsigned char j = 0; j < _astValidTasks[i]._astTargetStats.Size(); ++j)
        {
            if (_astValidTasks[i]._astTargetStats[j].IsReachCond())
            {
                continue;
            }

            iRet = _astValidTasks[i]._astTargetStats[j].CheckReachCond();
            CHECK_RETURN(iRet);

            if (_astValidTasks[i]._astTargetStats[j].IsReachCond())
            {
                m_bTaskTargetStatus = true;
            }
            /*
            if (_astValidTasks[i]._astTargetStats[j].IsReachCond())
            {
                iRet = CleanTrigger(_astValidTasks[i]._astTargetStats[j]._stCond._ucCond, _astValidTasks[i]._stTaskID);
                CHECK_RETURN(iRet);
            }
            */
        }   

        if (_astValidTasks[i].IsFinished())
        {
            continue;
        }

        iRet = _astValidTasks[i].CheckFinished();
        CHECK_RETURN(iRet);

        if (_astValidTasks[i].IsFinished())
        {
            //任务状态发生改变

            //触发任务集市完成后的操作
            CTaskID & stTaskID = _astValidTasks[i]._stTaskID;
            if (_uiMarketTaskID > 0)
            {
                CTaskID stMarketTask(_uiMarketTaskID);
                if (stTaskID == stMarketTask)
                {
                    CMainCtrl::m_pstMain->m_stMarketMgr.InfomTaskCompleted(_uiGID);
                }
            }

            //清理Trigger
            for (unsigned char j = 0; j < _astValidTasks[i]._astTargetStats.Size(); ++j)
            {
                iRet = CleanTrigger(_astValidTasks[i]._astTargetStats[j]._stCond._ucCond, _astValidTasks[i]._stTaskID);
                CHECK_RETURN(iRet);
            }
        }
    }

    return 0;
}

int CUserTask::CleanTrigger(unsigned char ucCondType, CTaskID& stTaskID)
{
    int iRet = 0;
    CT_TRACE(("清理触发器:"));
    CT_TRACE(("类型 %d", ucCondType));
    stTaskID.DumpTaskID();

    switch(ucCondType)
    {
    case CTaskCond::TASK_LEVEL_UP:
        iRet = CleanTrigger(_astLevelUpTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_BOSS_CONQUER:
        iRet = CleanTrigger(_astBossConquerTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_PET_LEVEL_UP:
        iRet = CleanTrigger(_astPetLevelUpTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_TASK_FINISH:
        iRet = CleanTrigger(_astFinishTaskTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_ENEMY_KILL:
        iRet = CleanTrigger(_astKillEnemyTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_DO_SOME_OPT:
        iRet = CleanTrigger(_astDoSomeOptTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_USE_ITEM:
        iRet = CleanTrigger(_astUseItemTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_GET_ITEM:
    case CTaskCond::TASK_GET_AND_COMMIT_ITEM:
        iRet = CleanTrigger(_astGetItemTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    case CTaskCond::TASK_FINISH_STAGE:
        iRet = CleanTrigger(_astFinishInstanceTriggers, stTaskID);
        CHECK_RETURN(iRet);
        break;
    default:
        break;
    }

    return 0;
}

int CUserTask::CleanTrigger(CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>& astTriggers, CTaskID& stTaskID)
{
    for (int i = 0; i < astTriggers.Size(); ++i)   //todo:可能有性能问题
    {
        if (astTriggers[i]._stTaskID == stTaskID)
        {
            CT_TRACE(("清理触发器被清理,任务信息: "));
            stTaskID.DumpTaskID();

            astTriggers.ShiftDelOneItem(i);
            --i;
        }
    }

    return 0;
}

bool CUserTask::IsCanAcceptTask(CTaskID& stTaskID)
{
    CT_TRACE(("已经激活的任务数 %d", _astOpenTaskIDs.Size()));
    for (int i = 0; i < _astOpenTaskIDs.Size(); ++i)
    {
        CT_TRACE(("检查当前任务:"));
        _astOpenTaskIDs[i].DumpTaskID();

        if (_astOpenTaskIDs[i] == stTaskID)
        {
            CT_TRACE(("在已经激活的任务中找到任务"));
            return true;
        }
    }

    CT_TRACE(("没有在已经激活的任务中找到任务"));
    return false;
}

int CUserTask::CleanOpenTask(CTaskID& stTaskID)
{
    for (int i = 0; i < _astOpenTaskIDs.Size(); ++i)
    {
        if (_astOpenTaskIDs[i] == stTaskID)
        {
            CT_TRACE(("激活任务被清理，任务信息："));
            stTaskID.DumpTaskID();

            _astOpenTaskIDs.ShiftDelOneItem(i);
            return 0;
        }
    }

    return 0;
}

int CUserTask::CommitTask(CTaskID& stTaskID)
{
    int iRet = 0;

    CT_TRACE(("提交任务：In CUserTask::CommitTask()"));

    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        if (_astValidTasks[i]._stTaskID == stTaskID)
        {
            if (!_astValidTasks[i].IsFinished())
            {
                CT_TRACE(("任务目标未完成"));
                return ERROR_TASK_COMMIT_NOT_FINISHED;
            }   
            else
            {
                //清理任务
                _astValidTasks.ShiftDelOneItem(i);

                CT_TRACE(("清理任务:"));
                stTaskID.DumpTaskID();

                //记录历史
                iRet = _stTaskStatus.SetTaskFinished(stTaskID);
                CHECK_RETURN(iRet);

                return 0;
            }
        }
    }

    return ERROR_TASK_COMMIT_NO_TASK;
}

bool CUserTask::IsValidTask(CTaskID& stTaskID)
{
    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        if (_astValidTasks[i]._stTaskID == stTaskID)
        {
            return true;
        }
    }

    return false;
}

void CUserTask::DumpTaskInfo()
{
    CT_TRACE(("已经激活的任务:"));
    for (int i = 0; i < _astOpenTaskIDs.Size(); ++i)
    {
        CT_TRACE(("是否是主线任务 %d;  任务ID %d; 分支号 %d", 
            _astOpenTaskIDs[i].IsMainTask(), _astOpenTaskIDs[i]._uiTaskID, _astOpenTaskIDs[i]._uiBranchID));
    }

    CT_TRACE(("进行中的任务:"));
    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        CT_TRACE(("是否是主线任务 %d;  任务ID %d; 分支号 %d", 
            _astValidTasks[i]._stTaskID.IsMainTask(), _astValidTasks[i]._stTaskID._uiTaskID, _astValidTasks[i]._stTaskID._uiBranchID));

        for (int j = 0; j < _astValidTasks[i]._astTargetStats.Size(); ++j)
        {
            CT_TRACE(("\t是否完成目标 %d; 目标类型 %d; DATA1 %d; DATA2 %d", 
                _astValidTasks[i]._astTargetStats[j]._bIsReachCond,
                _astValidTasks[i]._astTargetStats[j]._stCond._ucCond,
                _astValidTasks[i]._astTargetStats[j]._stCond._iCondData1,
                _astValidTasks[i]._astTargetStats[j]._stCond._iCondData2));
            CT_TRACE(("\t\t当前状态: 目标类型 %d; DATA1 %d; DATA2 %d", 
                _astValidTasks[i]._astTargetStats[j]._stNowStat._ucCond,
                _astValidTasks[i]._astTargetStats[j]._stNowStat._iCondData1,
                _astValidTasks[i]._astTargetStats[j]._stNowStat._iCondData2));
        }
    }

    CT_TRACE(("等待激活的任务:"));
    for (int i = 0; i < _astWaitForOpenTasks.Size(); ++i)
    {
        CT_TRACE(("是否是主线任务 %d;  任务ID %d; 分支号 %d", 
            _astWaitForOpenTasks[i]._stTaskID.IsMainTask(), _astWaitForOpenTasks[i]._stTaskID._uiTaskID, _astWaitForOpenTasks[i]._stTaskID._uiBranchID));

        for (int j = 0; j < _astWaitForOpenTasks[i]._astTargetStats.Size(); ++j)
        {
            CT_TRACE(("\t是否完成目标 %d; 目标类型 %d; DATA1 %d; DATA2 %d", 
                _astWaitForOpenTasks[i]._astTargetStats[j]._bIsReachCond,
                _astWaitForOpenTasks[i]._astTargetStats[j]._stCond._ucCond,
                _astWaitForOpenTasks[i]._astTargetStats[j]._stCond._iCondData1,
                _astWaitForOpenTasks[i]._astTargetStats[j]._stCond._iCondData2));
            CT_TRACE(("\t\t当前状态: 目标类型 %d; DATA1 %d; DATA2 %d", 
                _astWaitForOpenTasks[i]._astTargetStats[j]._stNowStat._ucCond,
                _astWaitForOpenTasks[i]._astTargetStats[j]._stNowStat._iCondData1,
                _astWaitForOpenTasks[i]._astTargetStats[j]._stNowStat._iCondData2));
        }
    }
}

void CUserTask::DumpTriggerInfo()
{
    CT_TRACE(("等级上升触发器:"));
    for (int i = 0; i < _astLevelUpTriggers.Size(); ++i)
    {
        CT_TRACE(("用途 %d; 是否是主线任务 %d;  任务ID %d; 分支号 %d; 任务索引 %d, 目标索引 %d",
            _astLevelUpTriggers[i]._ucTargetType, _astLevelUpTriggers[i]._stTaskID.IsMainTask(),
            _astLevelUpTriggers[i]._stTaskID._uiTaskID, _astLevelUpTriggers[i]._stTaskID._uiBranchID,
            _astLevelUpTriggers[i]._ucTaskIndex, _astLevelUpTriggers[i]._ucTargetIndex));
    }
}

int CUserTask::Init(CUserTaskData& stUserTaskData)
{
    int iRet = 0;
    Clean();

    iRet = _stTaskStatus.Init(stUserTaskData._stTaskStatus);
    CHECK_RETURN(iRet);

    for (int i = 0; i < stUserTaskData._astOpenTaskIDs.Size(); ++i)
    {
        CTaskID stTaskID;
        iRet = stTaskID.Init(stUserTaskData._astOpenTaskIDs[i]);
        CHECK_RETURN(iRet);
        iRet = AddOpenTaskData(stTaskID);
        CHECK_RETURN(iRet);
    }

    for (int i = 0; i < stUserTaskData._astValidTasks.Size(); ++i)
    {
        CValidTask stValidTask;
        iRet = stValidTask.Init(stUserTaskData._astValidTasks[i]);
        CHECK_RETURN(iRet);
        iRet = AddValidTaskData(stValidTask);
        CHECK_RETURN(iRet);
    }

    for (int i = 0; i < stUserTaskData._astWaitForOpenTasks.Size(); ++i)
    {
        CTaskWaitForOpen stTaskWaitForOpen;
        iRet = stTaskWaitForOpen.Init(stUserTaskData._astWaitForOpenTasks[i]);
        CHECK_RETURN(iRet);
        iRet = AddWaitForOpenData(stTaskWaitForOpen);
        CHECK_RETURN(iRet);
    }

    return 0;
}

int CUserTask::GetData(CUserTaskData& stUserTaskData)
{
    int iRet = 0;
    stUserTaskData.Clear();

    iRet = _stTaskStatus.GetData(stUserTaskData._stTaskStatus);
    CHECK_RETURN(iRet);

    for (int i = 0; i < _astOpenTaskIDs.Size(); ++i)
    {
        CTaskIDData stTaskIDData;
        iRet = _astOpenTaskIDs[i].GetData(stTaskIDData);
        CHECK_RETURN(iRet);

        stUserTaskData._astOpenTaskIDs.AddOneItem(stTaskIDData);
    }

    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        CValidTaskData stValidTaskData;
        iRet = _astValidTasks[i].GetData(stValidTaskData);
        CHECK_RETURN(iRet);

        stUserTaskData._astValidTasks.AddOneItem(stValidTaskData);
    }

    for (int i = 0; i < _astWaitForOpenTasks.Size(); ++i)
    {
        CTaskWaitForOpenData stWaitForOpenData;
        iRet = _astWaitForOpenTasks[i].GetData(stWaitForOpenData);
        CHECK_RETURN(iRet);

        stUserTaskData._astWaitForOpenTasks.AddOneItem(stWaitForOpenData);
    }

    return 0;
}

bool CUserTask::IsTaskFinished(CTaskID& stTaskID)
{
    return _stTaskStatus.IsFinished(stTaskID);
}

void CUserTask::SetUserGid(unsigned int uiGid)
{
	this->_uiGID = uiGid;
}

unsigned int CUserTask::GetUserGid() const
{
	return this->_uiGID;
}

int CUserTask::GetTaskAwardItems(CTaskConfigData& curTaskCfgData, CDropItem& stDropItem)
{
	int iRet = 0;
	CDropItemData stDropItemData;

	for (int i = 0; i < curTaskCfgData._astTaskAward.Size(); ++i)
	{
		const CTaskAward & OneTaskAward = curTaskCfgData._astTaskAward[i];
		if( OneTaskAward.m_iAwardItemId==TASK_AWARD_SETCLASS )
		{
			CMainCtrl::m_pstMain->m_stItemMgr.GetDropItemData(GetUserGid(), OneTaskAward.m_iAwardType, 
						0, OneTaskAward.m_strItemSet.c_str(), COMM_MOD, OneTaskAward.m_iAwardData, stDropItemData);
		}else
		{
			CMainCtrl::m_pstMain->m_stItemMgr.GetDropItemData(GetUserGid(), OneTaskAward.m_iAwardType, 
						OneTaskAward.m_iAwardItemId, "", COMM_MOD, OneTaskAward.m_iAwardData, stDropItemData);
		}
		stDropItem.Drop(stDropItemData);
	}
	return iRet;
}

int CUserTask::GMSetTaskFinished(CTaskID & stTaskID)
{
    int iRet = 0;

    iRet = this->_stTaskStatus.SetTaskFinished( stTaskID );

    // 清理可接任务
    for( int i=0; i<_astOpenTaskIDs.Size(); ++i )
    {
        if (stTaskID._uiBranchID != _astOpenTaskIDs[i]._uiBranchID)
        {
            continue;
        }

        if (stTaskID._uiTaskID >= _astOpenTaskIDs[i]._uiTaskID)
        {
            _astOpenTaskIDs.ShiftDelOneItem(i);
        }

        break;
    }

    //清理待激活任务
    for (int i = 0; i < _astWaitForOpenTasks.Size(); ++i)
    {
        if (stTaskID._uiBranchID != _astWaitForOpenTasks[i]._stTaskID._uiBranchID)
        {
            continue;
        }
        
        if (stTaskID._uiTaskID >= _astWaitForOpenTasks[i]._stTaskID._uiTaskID)
        {
            //清理触发器
            CTaskWaitForOpen& stWaitForOpenTask = _astWaitForOpenTasks[i];
            for (int j = 0; j < stWaitForOpenTask._astTargetStats.Size(); ++j)
            {
                CleanTrigger(stWaitForOpenTask._astTargetStats[j]._stCond._ucCond, stWaitForOpenTask._stTaskID);
            }

            _astWaitForOpenTasks.ShiftDelOneItem(i);
        }

        break;
    }

    //清理已接任务
    for( int i=0; i<_astValidTasks.Size(); ++i )
    {
        if (stTaskID._uiBranchID != _astValidTasks[i]._stTaskID._uiBranchID)
        {
            continue;
        }

        if (stTaskID._uiTaskID >= _astValidTasks[i]._stTaskID._uiTaskID)
        {
            //清理触发器
            CValidTask& stValidTask = _astValidTasks[i];
            for (int j = 0; j < stValidTask._astTargetStats.Size(); ++j)
            {
                CleanTrigger(stValidTask._astTargetStats[j]._stCond._ucCond, stValidTask._stTaskID);
            }

            _astValidTasks.ShiftDelOneItem(i);
        }

        break;
    }

    return iRet;
}

bool CUserTask::HasDoSomeOptTask()
{
    for (int i = 0; i < _astValidTasks.Size(); ++i)
    {
        for (int j = 0; j < _astValidTasks[i]._astTargetStats.Size(); ++j)
        {
            if (CTaskCond::TASK_DO_SOME_OPT == _astValidTasks[i]._astTargetStats[j]._stCond._ucCond)
            {
                return true;
            }
        }
    }

    return false;
}

void CUserTask::CheckTaskHasFinished(CTaskID stTaskID)
{
    if (!_stTaskStatus.IsFinished(stTaskID))
    {
        return;
    }

    for (int i = 0; i < _astOpenTaskIDs.Size(); ++i)
    {
        if (_astOpenTaskIDs[i] == stTaskID)
        {
            _astOpenTaskIDs.DelOneItem(i);
            m_bTaskTargetStatus = true;
            this->UpdateTaskStatus();
            return;
        }
    }
}

void CUserTask::SetMarketTaskID (unsigned int uiMarketTaskID)
{
    if (_uiMarketTaskID != 0)
    {
        CT_ERROR(("old: %d, new: %d", _uiMarketTaskID, uiMarketTaskID));   //重复设置集市任务
    }

    _uiMarketTaskID = uiMarketTaskID;
}

unsigned int CUserTask::GetMarketTaskID ()
{
    return _uiMarketTaskID;
}
